package com.jack.helloworld.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.jack.helloworld.R;

import java.util.Calendar;

public class CalendarActivity extends AppCompatActivity implements DatePicker.OnDateChangedListener,
        TimePicker.OnTimeChangedListener, View.OnClickListener,
        DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    Calendar calender;
    DatePicker datePicker;
    TimePicker timePicker;

    Button btnDateDialog;
    Button btnTimeDialog;

    int year;
    int mouth;
    int day;
    int hour;
    int minute;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);

        initView();
        initDate();
    }

    private void initView() {
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        btnDateDialog = (Button) findViewById(R.id.dateDialog);
        btnTimeDialog = (Button) findViewById(R.id.timeDialog);

    }

    private void initDate() {
        calender = Calendar.getInstance();
        year = calender.get(Calendar.YEAR);
        mouth = calender.get(Calendar.MONTH);
        day = calender.get(Calendar.DAY_OF_MONTH);
        hour = calender.get(Calendar.HOUR_OF_DAY);
        minute = calender.get(Calendar.MINUTE);

        setTitle(year + "-" + (mouth + 1) + "-" + day + " " + hour + ":" + minute);

        btnDateDialog.setOnClickListener(this);
        btnTimeDialog.setOnClickListener(this);
        datePicker.init(year, mouth, day, this);
        timePicker.setOnTimeChangedListener(this);

    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        this.year = year;
        this.mouth = monthOfYear;
        this.day = dayOfMonth;
        setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + " " + hour + ":" + minute);

    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        this.hour = hourOfDay;
        this.minute = minute;
        setTitle(year + "-" + (mouth + 1) + "-" + day + " " + hourOfDay + ":" + minute);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dateDialog:
                new DatePickerDialog(this, this, year, mouth, day).show();

                break;
            case R.id.timeDialog:
                new TimePickerDialog(this, this, hour, minute, true).show();
                break;
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        this.year = year;
        this.mouth = monthOfYear;
        this.day = dayOfMonth;

        setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + " " + hour + ":" + minute);

    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        this.hour = hourOfDay;
        this.minute = minute;

        setTitle(year + "-" + (mouth + 1) + "-" + day + " " + hourOfDay + ":" + minute);

    }
}
